package com.egg.mall.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.mall.entity.advert.Advert;
import com.egg.mall.service.advert.IAdvertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.stream.Collectors;


/**
 * @author east
 * @since 2023-05-09
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "广告中心")
@RestController
@RequestMapping("/advert")
public class WebAdvertController extends WebController {


    @Autowired
    private IAdvertService advertService;


    @ApiOperation(value = "广告表新增")
    @PostMapping
    public ResponseData<Long> advertPost(@RequestBody Advert param) {
        advertService.baseAdd(param);
        return ResponseData.success(param.getId());
    }

    @ApiOperation(value = "广告表分页查询")
    @GetMapping("/page")
    public ResponseData<PageData<Advert>> advertPageGet(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        LambdaQueryWrapper<Advert> qw = advertService.buildLambdaSortQw();
        Page<Advert> dbPage = advertService.page(new Page<>(current, pageSize), qw);
        return ResponseData.success(new PageData<>(dbPage));
    }

    @ApiOperation(value = "广告表详情查询")
    @GetMapping("/{id}/info")
    public ResponseData<Advert> advertInfoGet(
            @ApiParam(value = "广告表主键ID", required = true) @PathVariable String id
    ) {
        Advert dbInfo = advertService.baseGetById(id);
        return ResponseData.success(dbInfo);
    }

    @ApiOperation(value = "广告表覆盖更新")
    @PutMapping("/{id}")
    public ResponseData<String> advertPut(
            @ApiParam(value = "广告表主键ID", required = true) @PathVariable String id
            , @RequestBody Advert param
    ) {
        Advert exist = advertService.baseGetById(id);
        param.setId(exist.getId());
        advertService.baseEdit(param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "广告表部分更新")
    @PatchMapping("/{id}")
    public ResponseData<String> advertPatch(
            @ApiParam(value = "广告表主键ID", required = true) @PathVariable String id
            , @RequestBody Advert param
    ) {
        Advert exist = advertService.baseGetById(id);
        param.setId(exist.getId());
        advertService.baseEdit(param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "广告表删除")
    @DeleteMapping
    public ResponseData<String> advertDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        advertService.baseDel(
                ids.stream().filter(o -> ObjectUtil.isNotEmpty(o)).collect(Collectors.toSet())
        );
        return ResponseData.success();
    }


}

